package org.hgvc.webapp.ets.jsf.beans.finance;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.bouncycastle.asn1.x509.qualified.ETSIQCObjectIdentifiers;
import org.hgvc.webapp.ets.bo.AdvancePayment;
import org.hgvc.webapp.ets.bo.Invoice;
import org.hgvc.webapp.ets.bo.InvoiceDtl;
import org.hgvc.webapp.ets.bo.MemberComp;
import org.hgvc.webapp.ets.bo.MemberInd;
import org.hgvc.webapp.ets.bo.MemberInstallmentPlan;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.spring.service.FinanceService;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class MemberAccountBean extends AbstractBean{
	public static final String BEAN_NAME="memberAccountBean";
	
	private MemberService memberService;
	private FinanceService financeService;
	private List<MemberInd> memberIndList=new ArrayList<MemberInd>();
	private List<MemberComp> memberCompList=new ArrayList<MemberComp>();
	private List<InvoiceDtl> invoiceDtlList=new ArrayList<InvoiceDtl>();
	private List<MemberInstallmentPlan> memberInstallmentPlanList = new ArrayList<MemberInstallmentPlan>();
	private List<SelectItem> selectInstallementNoList = new ArrayList<SelectItem>();
	private MemberInd memberInd;
	private boolean catchError;
	
	private String membershipNo;
	private String memberName;
	private String icNo;
	private double totalAdvancePayment;
	private double totalCollectedAmount;
	private double totalBalance;
	private String installmentPlanId;
	private MemberInstallmentPlan memberInstallmentPlan;
	
	private Invoice invoice;
	
	public String getInstallmentPlanId() {
		return installmentPlanId;
	}

	public void setInstallmentPlanId(String installmentPlanId) {
		this.installmentPlanId = installmentPlanId;
	}

	public double getTotalAdvancePayment() {
		return totalAdvancePayment;
	}

	public void setTotalAdvancePayment(double totalAdvancePayment) {
		this.totalAdvancePayment = totalAdvancePayment;
	}

	public double getTotalCollectedAmount() {
		return totalCollectedAmount;
	}

	public void setTotalCollectedAmount(double totalCollectedAmount) {
		this.totalCollectedAmount = totalCollectedAmount;
	}

	public double getTotalBalance() {
		return totalBalance;
	}

	public void setTotalBalance(double totalBalance) {
		this.totalBalance = totalBalance;
	}

	public void initSearchMember(){
		memberIndList = new ArrayList<MemberInd>();
	}
	
	public String getMembershipNo() {
		return membershipNo;
	}
	public void setMembershipNo(String membershipNo) {
		this.membershipNo = membershipNo;
	}
	public String getMemberName() {
		return memberName;
	}
	public void setMemberName(String memberName) {
		this.memberName = memberName;
	}
	public String getIcNo() {
		return icNo;
	}
	public void setIcNo(String icNo) {
		this.icNo = icNo;
	}
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public void setFinanceService(FinanceService financeService) {
		this.financeService = financeService;
	}
	public List<MemberInd> getMemberIndList() {
		return memberIndList;
	}
	public void setMemberIndList(List<MemberInd> memberIndList) {
		this.memberIndList = memberIndList;
	}
	public List<MemberComp> getMemberCompList() {
		return memberCompList;
	}
	public void setMemberCompList(List<MemberComp> memberCompList) {
		this.memberCompList = memberCompList;
	}
	
	public String searchMemberIndAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void searchMemberIndActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.memberIndList=memberService.getMemberIndList(this.membershipNo, this.memberName, this.icNo);
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	public String searchMemberIndForInvoicingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public String clearSearchAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void clearSearchActionEvent(ActionEvent event){
		this.memberName="";
		this.membershipNo="";
		this.icNo="";
	}
	
	public String viewMemberIndAccountViewAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void viewMemberIndAccountViewActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			UIParameter param = (UIParameter) event.getComponent().findComponent("memberIndId");
			String memberIndId = (String) param.getValue();
		
			for (MemberInd memberInd : this.memberIndList){
				if (memberInd.getMemberIndId().equalsIgnoreCase(memberIndId))
					this.memberInd = memberInd;
			}
			
			this.invoiceDtlList=financeService.getInvoiceDtlListByMemberId(this.memberInd.getMember().getMemberId());
			
			AdvancePayment advancePayment = financeService.getAdvancePaymentByMemberId(this.memberInd.getMember().getMemberId());
			if (advancePayment!=null)
				this.totalAdvancePayment=advancePayment.getAmount();
			
			Double collectedAmount = financeService.getInvoiceCollectedAmountByMemberId(this.memberInd.getMember().getMemberId());
			if (collectedAmount!=null)
				this.totalCollectedAmount=collectedAmount;
			
			Double invAmount = financeService.getInvoiceAmountByMemberId(this.memberInd.getMember().getMemberId());
			if (invAmount!=null){
				this.totalBalance=invAmount - collectedAmount;
			}
				
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	
	public MemberInd getMemberInd() {
		return memberInd;
	}
	public void setMemberInd(MemberInd memberInd) {
		this.memberInd = memberInd;
	}

	public List<InvoiceDtl> getInvoiceDtlList() {
		return invoiceDtlList;
	}

	public void setInvoiceDtlList(List<InvoiceDtl> invoiceDtlList) {
		this.invoiceDtlList = invoiceDtlList;
	}
	
	public String navMemberIndForInvoicingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void navMemberIndForInvoicingActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			UIParameter param = (UIParameter) event.getComponent().findComponent("memberIndId");
			String memberIndId = (String) param.getValue();
		
			for (MemberInd memberInd : this.memberIndList){
				if (memberInd.getMemberIndId().equalsIgnoreCase(memberIndId))
					this.memberInd = memberInd;
			}
			
			memberInstallmentPlanList=this.memberService.getMemberInstallmentPlanListByMemberId(memberInd.getMember().getMemberId());
			selectInstallementNoList=new ArrayList<SelectItem>();
			
			for (MemberInstallmentPlan memberInstallmentPlan : memberInstallmentPlanList){
				selectInstallementNoList.add(new SelectItem(memberInstallmentPlan.getMemberInstallmentPlanId(),memberInstallmentPlan.getInstallmentItem()));
			}
				
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}

	public String navConfirmMemberIndInvoiceAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void navConfirmMemberIndInvoiceActionEvent(ActionEvent event){
		this.catchError=false;
		
		for (MemberInstallmentPlan memberInstallmentPlan : memberInstallmentPlanList){
			if (this.installmentPlanId.equalsIgnoreCase(memberInstallmentPlan.getMemberInstallmentPlanId()))
				this.memberInstallmentPlan=memberInstallmentPlan;
		}
	}
	
	public String confirmMemberIndInvoiceAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void confirmMemberIndInvoiceActionEvent(ActionEvent event){
		this.catchError=false;
		
		try{
			this.invoice=financeService.generateInvoiceForMemberInd(this.memberInd, this.memberInstallmentPlan, getThisUser());
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public List<SelectItem> getSelectInstallementNoList() {
		return selectInstallementNoList;
	}

	public Invoice getInvoice() {
		return invoice;
	}

	public void setInvoice(Invoice invoice) {
		this.invoice = invoice;
	}

	public MemberInstallmentPlan getMemberInstallmentPlan() {
		return memberInstallmentPlan;
	}

	public void setMemberInstallmentPlan(MemberInstallmentPlan memberInstallmentPlan) {
		this.memberInstallmentPlan = memberInstallmentPlan;
	}
	
}
